import time

from common.Utils import FSDriver
from selenium.webdriver.common.by import By


# 登录界面
class Loginpage:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/sign-in.html"
        self.driver = FSDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(5)

    # 输入框清空
    def input_clear(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

    # 成功登录
    def succeed_login_test(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看是否登录成功
        FSDriver.getscreeshot()
        assert self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar")
        print("登录成功")

        self.driver.back()

    # 特殊登录，改过的密码
    def Special_login(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看是否登录成功
        FSDriver.getscreeshot()
        assert self.driver.find_element(By.CSS_SELECTOR, "#index_nav_avatar")
        print("登录成功")

    # 异常样例
    # 账号、密码错误
    def fail_name_pass(self):
        val_url = self.driver.current_url

        self.input_clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看是否登录成功
        FSDriver.getscreeshot()
        assert self.driver.current_url == val_url
        assert self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div.col-lg.d-none.d-lg-block > img")
        print("登录失败，请重试")

        self.driver.refresh()

    # 密码错误
    def fail_pass(self):
        val_url = self.driver.current_url

        self.input_clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书尽")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看是否登录成功
        FSDriver.getscreeshot()
        assert self.driver.current_url == val_url
        assert self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div.col-lg.d-none.d-lg-block > img")
        print("登录失败，请重试")

        self.driver.refresh()

    # 账号错误
    def fail_name(self):
        val_url = self.driver.current_url

        self.input_clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("毕书")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 查看是否登录成功
        FSDriver.getscreeshot()
        assert self.driver.current_url == val_url
        assert self.driver.find_element(By.CSS_SELECTOR, "body > div > div > div > div.col-lg.d-none.d-lg-block > img")
        print("登录失败，请重试")

        self.driver.refresh()

    # 输入框为空
    def null_input(self):
        self.input_clear()

        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        if self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-3 > div") or self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-2 > div > div"):
            print("登录失败，请重试")

    # 异常登录
    def fail_login_test(self):
        self.fail_name_pass()
        self.fail_pass()
        self.fail_name()
        self.null_input()
